//删除元素
function HintRemove(obj){
	$("#"+obj).remove();
}

//添加进购物车
function CartAdd(obj, webpath, linktype, linkurl, good_id) {
    if ($("#goods_id").val() == "" || $("#goods_quantity").val() == "") {
        return false;
    }
    $.ajax({
        type: "post",
        url: webpath + "tools/submit_ajax.ashx?action=cart_goods_add",
        data: {
            goods_id: function () {
                if (good_id != undefined && good_id != null) {
                    return good_id;
                } else {
                    return $("#goods_id").val();
                }
            },
            goods_quantity: function () {
                return $("#goods_quantity").val();
            }
        },
        dataType: "json",
        beforeSend: function (XMLHttpRequest) {
            //发送前动作
        },
        success: function (data, textStatus) {
            if (data.msg == 1) {

                if (linktype == 1) {
                    
                    location.href = linkurl;
                } else {
                    $.ligerDialog.error(data.msgbox);
                    //$("#cart_count").html(data.quantity);

                }
            } else {
                $("#cart_info_hint").remove();
                var HintHtml = '<div id="cart_info_hint" class="msg_tips cart_info">'
					+ '<div class="ico error"></div>'
					+ '<div class="msg">'
					+ '<strong>商品添加到购物车失败！</strong>'
					+ '<p>' + data.msgbox + '</p>'
					+ '<a class="close" title="关闭" href="javascript:void(0);" onclick="HintRemove(\'cart_info_hint\');"><span>关闭</span></a>'
					+ '</div>'
					+ '</div>'
                $(obj).after(HintHtml); //添加节点
                //alert(data.msgbox);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("状态：" + textStatus + "；出错提示：" + errorThrown);
        },
        timeout: 20000
    });
    return false;
}

function CartAdd1(obj, webpath, linktype, linkurl, good_id) {

    if ($("#goods_id").val() == "" || $("#goods_quantity").val() == "") {
        return false;
    }
    $.ajax({
        type: "post",
        url: "/shopping/addcart",
        data: {
            pid: function () {
                if (good_id != undefined && good_id != null) {
                    return good_id;
                } else {
                    return $("#goods_id").val();
                }
            },
            num: function () {
                return $("#goods_quantity").val();
            }
        },
        dataType: "json",
        beforeSend: function (XMLHttpRequest) {
            //发送前动作
        },
        success: function (data, textStatus) {
            if (data.ret == 1) {
                if (linktype == 0) {
                    $.ligerDialog.success(data.msg);
                } else {
                    location.href = '/shopping/cart';
                }


            } else {
                $.ligerDialog.error(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("状态：" + textStatus + "；出错提示：" + errorThrown);
        },
        timeout: 20000
    });
    return false;
}


function DeleteSelectCart(webpath) {
    if (confirm("确定要删除所选项目？")) {
        var checkedList = new Array();
        $("input[name='cart_id']:checked").each(function () {
        checkedList.push($(this).val());
         });
                $.ajax({
                    type: "post",
                    url: webpath + "tools/submit_ajax.ashx?action=cart_goods_delete_select",
                    data: {
                        goods_id: checkedList.toString()
                    },
                    dataType: "json",
                    beforeSend: function (XMLHttpRequest) {
                        //发送前动作
                    },
                    success: function (data, textStatus) {
                        if (data.msg == 1) {
                            location.reload();
                        } else {
                            alert(data.msgbox);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("状态：" + textStatus + "；出错提示：" + errorThrown);
                    },
                    timeout: 20000
                });
    }
}

//删除购物车商品
function DeleteCart(obj, webpath, goods_id){
	if(!confirm("您确认要从购物车中移除吗？") || goods_id==""){
		return false;
	}
	$.ajax({
		type: "post",
		url: webpath + "tools/submit_ajax.ashx?action=cart_goods_delete",
		data: {
			goods_id: function() {
				return goods_id;
			}
		},
		dataType: "json",
		beforeSend: function(XMLHttpRequest) {
			//发送前动作
		},
		success: function(data, textStatus) {
			if (data.msg == 1) {
				location.reload();
			} else {
				alert(data.msgbox);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert("状态：" + textStatus + "；出错提示：" + errorThrown);
		},
		timeout: 20000
	});
	return false;
}

//计算购物车金额
function CartAmountTotal(obj, webpath, goods_id) {
    if(isNaN($(obj).val())){
		alert('商品数量只能输入数字!');
		$(obj).val("1");
	}
	$.ajax({
	    type: "post",
	    url: "/shopping/ChangeCart",
	    data: {
	        id: function () {
	            return goods_id;
	        },
	        num: function () {
	            return $(obj).val();
	        }
	    },
	    dataType: "json",
	    beforeSend: function (XMLHttpRequest) {
	        //发送前动作
	    },
	    success: function (data, textStatus) {
	        if (data.ret == 1) {
	            //alert(data.msgbox);
	            location.reload();
	        } else {
	            alert(data.msg);
	            location.reload();
	        }
	    },
	    error: function (XMLHttpRequest, textStatus, errorThrown) {
	        alert("状态：" + textStatus + "；出错提示：" + errorThrown);
	    },
	    timeout: 20000
	});
	return false;
}
//购物车数量加减
function CartComputNum(obj, webpath, goods_id, num) {
	if(num > 0){
	    var goods_quantity = $(obj).next("input[name='goods_quantity']");
	    $(goods_quantity).val(parseInt($(goods_quantity).val()) + 1);
	    //alert($(goods_quantity).val());
	    //return;
		//计算购物车金额
		CartAmountTotal($(goods_quantity), webpath, goods_id);
	}else{
    var goods_quantity = $(obj).prev("input[name='goods_quantity']");
		if(parseInt($(goods_quantity).val()) > 1){
			$(goods_quantity).val(parseInt($(goods_quantity).val()) - 1);
			//计算购物车金额
			CartAmountTotal($(goods_quantity), webpath, goods_id);
		}
	}
}
//计算支付手续费总金额
function PaymentAmountTotal(obj){
	var payment_price = $(obj).next("input[name='payment_price']").val();
	$("#payment_fee").text(payment_price); //运费
	if ($(obj).val() == "5") {
	    $("#p_point").show();
	}
	else {
	    $("#p_point").hide();
	}

	$("#hid_paytype_id").val(payment_price);
	OrderAmountTotal();
}
//计算配送费用总金额
function FreightAmountTotal(obj) {
	var distribution_price = $(obj).next("input[name='distribution_price']").val();
	$("#freight_amount").text(distribution_price); //运费
	$("#hid_distribution_id").val($(obj).val());
	
	OrderAmountTotal();
}
//计算订单总金额
function OrderAmountTotal(){
	var goods_amount = $("#goods_amount").text(); //商品总金额
	var payment_fee = $("#payment_fee").text(); //手续费
	var freight_amount = $("#freight_amount").text(); //运费
	var order_amount = parseFloat(goods_amount) + parseFloat(payment_fee) + parseFloat(freight_amount); //订单总金额 = 商品金额 + 手续费 + 运费
	$("#order_amount").text(order_amount.toFixed(2));
}

/*搜索查询*/
function SiteSearch(send_url, divTgs) {
    var str = $.trim($(divTgs).val());
    if (str.length > 0 && str != "输入关健字") {
        window.location.href = send_url + "?keyword=" + encodeURI($(divTgs).val()) + "&val=" + encodeURI($(divTgs).val());
    }
    return false;
}